Mysterious different conversion to string[] of seemingly same input data

Posted by Roman Kuzmin on Stack Overflow See other posts from Stack Overflow or by Roman Kuzmin
Published on 2010-04-30T11:05:41Z Indexed on 2010/04/30 13:07 UTC
Read the original article Hit count: 169

Filed under:
|

During investigation of some problem I found that the reason was unexpected different conversion to string[] of seemingly same input data. Namely, in the code below two commands both return the same two items File1.txt and File2.txt. But conversion to string[] gives different results, see the comments.

Any ideas why is it? This might be a bug. If anybody also thinks so, I’ll submit it. But it would nice to understand what’s going on and avoid traps like that.

# *** WARNING
# *** Make sure you do not have anything in C:\TEMP\Test
# *** The code creates C:\TEMP\Test with File1.txt, File2.txt

# Make C:\TEMP\Test and two test files
$null = mkdir C:\TEMP\Test -Force
1 | Set-Content C:\TEMP\Test\File1.txt
1 | Set-Content C:\TEMP\Test\File2.txt

# This gets just file names
[string[]](Get-ChildItem C:\TEMP\Test)

# This gets full file paths
[string[]](Get-ChildItem C:\TEMP\Test -Include *)

# Output:
# File1.txt
# File2.txt
# C:\TEMP\Test\File1.txt
# C:\TEMP\Test\File2.txt

© Stack Overflow or respective owner

Related posts about powershell

Related posts about powershell-v2.0